﻿
var IsIn = function (obj, parentObjId) { while (obj) { if (obj.id == parentObjId) { return true; } obj = obj.parentNode; } return false; }
var Class = { create: function () { return function () { this.initialize.apply(this, arguments); } } }
var YJPopup = Class.create();
if (!parent.Zindex) {
    parent.Zindex = 1000;
}
else {
    parent.Zindex++;
}

YJPopup.prototype = {
    myContenter: new Object(),
    myContentDiv: new Object(),
    myBgDiv: new Object(),
    myDragBarDiv: new Object(),
    initialize: function (options) {
        //alert('4');
        this.ready = false;
        this.ISIE6 = $.browser.version < 8.0;
        this.dragBarDivID = options.dragBarDivID;
        if (options.autoCreate) { this.Create(options.contentDivID, options.dragBarDivID); }
        if (!options.contentDivID) { alert("未设置模式窗口的内容！"); return; }
        else { this.$contentDiv = $("#" + options.contentDivID); }
        this.$bgDiv = $(document.createElement("div")).appendTo(document.body);
        this.bgDivBgColor = options.bgDivBgColor ? options.bgDivBgColor : (this.ISIE6 ? "#ffffff" : "#000000");
        this.Y = typeof options.Y != "undefined" ? options.Y : 130;
        this.X = typeof options.X != "undefined" ? options.X : 130;
        this.dragable = !!options.dragable;
        this.$dragBarDiv = options.dragBarDivID ? $("#" + options.dragBarDivID) : null;
        this.$popupIframe = this.ISIE6 ? $(document.createElement("iframe")).attr({ name: "BgIframe", border: 0, frameborder: 0 }).appendTo(this.$contentDiv) : null;
        this.LoadingPath = options.LoadingPath ? options.LoadingPath : "../loading.htm";
        if (this.dragable && this.$dragBarDiv == null) { alert("not set the handle"); return; }
        this.ready = true;
        this.defaultBgCss = { "display": "none", "position": "absolute", "_position": "absolute", "opacity": "0.1", "-khtml-opacity": "0.1", "filter": "alpha(opacity=10)", "-moz-opacity": "0.1", "height": $(document).height() + "px", "width": "100%", "top": "0px", "left": "0px", "background-color": this.bgDivBgColor, "z-index": "100" };
        this.defaultContentCss = { "display": "none", "position": "absolute", "_position": "absolute", "z-index": "101", "top": this.Y, "left": this.X };
        this.SetCss();
    },
    SetCss: function () {
        this.$bgDiv.css(this.defaultBgCss);
        this.$contentDiv.css(this.defaultContentCss);
        if (this.$popupIframe != null)
        { this.$popupIframe.css({ "position": "absolute", 'z-index': "-1", "left": "0px", "top": "0px", "width": "100%", "height": this.$contentDiv.height() + "px" }); }
    },
    SetDragable: function () {
        if (this.dragable) {
            var $contentDiv = this.myContentDiv, $bgDiv = this.myBgDiv;
            var mycontenter = this.myContenter;
            this.myDragBarDiv.bind("mousedown", function (e) {
                if (mycontenter.document.body.setCapture) { mycontenter.document.body.setCapture(); };
                var offset = $(this).offset(), x = e.pageX - offset.left, y = e.pageY - offset.top, maxX = $(mycontenter.document).width() - $contentDiv.width();

                $(mycontenter.document.body).bind("mousemove", function (ev) {
                    // $(document.body).bind("mousemove", function (ev) {
                    var _x = ev.pageX - x, _y = ev.pageY - y;
                    if (_x <= maxX) { $contentDiv.css({ "left": _x + "px" }); }
                    if (_y >= 0) { $contentDiv.css({ "top": _y + "px" }); }
                });
            });

            $(mycontenter.document.body).mouseup(function () { $(this).unbind("mousemove"); if (mycontenter.document.body.releaseCapture) { mycontenter.document.body.releaseCapture(); } });
        }
    },

    SetPosition: function (_x, _y) {

        var $newcontentDiv = this.myContentDiv;
        var $newbgDiv = this.myBgDiv;
        var mycontenter = this.myContenter;

        var x = _x;
        $newbgDiv.css({ "width": $(mycontenter.document).width(), "height": $(mycontenter.document).height() - 4 + "px" })
        x = ($(mycontenter).width() - $newcontentDiv.width()) / 2;

        if (x < 0) { x = 10; }
        $newcontentDiv.css({ "top": _y + "px", "left": x + $(mycontenter.document).scrollLeft() + "px" });
    },
    Show: function (w, h, title, path, isAncestor, tranfun) {
        if (this.ready) {

            if (typeof tranfun == "undefined") {
                tranfun = false;
            }
            else {
                tranfun = true;
            }
            if (typeof isAncestor == "undefined") {
                isAncestor = false;
            }
            var $contentDiv = this.$contentDiv;
            if (this.ISIE6) { $("select").each(function () { if (!IsIn(this, $contentDiv.attr("id"))) { $(this).attr("disabled", "disabled"); } }) };
            
            if (typeof w != "undefined") { this.$contentDiv.css("width", w); }
            if (typeof h != "undefined") { this.$contentDiv.css("height", h); }
            if (typeof title != "undefined") { this.$contentDiv.find("span[id^='PopupTitle']").text(title); }

            if (typeof path != "undefined") {
                var $iframe = this.$contentDiv.find("iframe[name!='BgIframe']");
                
                $iframe.css({ "width": "100%", "height": this.$contentDiv.height() - parseInt(this.$dragBarDiv.css("height")) + "px" });

                if (tranfun) {

                    var k = 0;
                    var popups1 = $(window.parent.document.body).find("[id^='" + $contentDiv.attr("id").substr(0, $contentDiv.attr("id").length - 1) + "']")
                    var popupCount1 = popups1.length
                    //alert(popups)
                    for (; k < popupCount1; k++) {
                        if ($(popups1.get(k)).attr("id") == $contentDiv.attr("id") + "contentDivNew") {
                            break;
                        }
                    }
                    k = k - 1;
                    if (path.indexOf('?') > 0) {
                        $iframe.attr("src", path + "&tranfun=" + "iframe" + $(popups1.get(k)).attr("id"));
                    }
                    else {
                        $iframe.attr("src", path + "?tranfun=" + "iframe" + $(popups1.get(k)).attr("id"));
                    }
                }
                else {
                    $iframe.attr("src", path);
                }
            }

            var i = 0;
            if (isAncestor) {

                var bgID = "bgDivNew" + $contentDiv.attr("id");
                var contentID = $contentDiv.attr("id") + "contentDivNew";

                $(window.parent.document.body).find("#" + bgID).remove();
                $(window.parent.document.body).find("#" + contentID).remove();

                this.myContenter = window.parent;



                //克隆遮罩层----每个popup都拥有自己的遮罩层 

                if ($(window.parent.document.body).find("#" + bgID).length == 0) {
                    if (this.ISIE6) {

                        this.myBgDiv = $(parent.document.createElement("div")).attr("id", bgID).css(this.defaultBgCss).css("z-index", parent.Zindex++);
                        $(window.parent.document.body).append(this.myBgDiv);

                    }
                    else {

                        this.myBgDiv = this.$bgDiv.clone(true).attr("id", bgID).css("z-index", parent.Zindex++).appendTo(window.parent.document.body);
                    }

                }
                else {
                    this.myBgDiv = $(window.parent.document.body).find("#" + bgID);
                }
                //没内容层时加
                if ($(window.parent.document.body).find("#" + contentID).length == 0) {
                    if (this.ISIE6) {
                        this.myContentDiv = $(parent.document.createElement("div")).css(this.defaultContentCss).css("z-index", parent.Zindex++)
                                                .attr('id', contentID).css("width", w).css("height", h)
                                                .addClass("modal-header").html($contentDiv.html())
                                                .appendTo(window.parent.document.body);
                    }
                    else {
                        this.myContentDiv = this.$contentDiv.clone(true).attr("id", contentID).css("z-index", parent.Zindex++).appendTo(window.parent.document.body);
                    }
                }
                else {
                    this.myContentDiv = $(window.parent.document.body).find("#" + contentID);
                }

                this.myDragBarDiv = this.myContentDiv.find(".modal-header");
                this.myDragBarDiv.attr("id", "dragBarDivNew" + $contentDiv.attr("id"));

                var popups = $(window.parent.document.body).find("[id^='" + $contentDiv.attr("id").substr(0, $contentDiv.attr("id").length - 1) + "']")
                var popupCount = popups.length

                for (; i < popupCount; i++) {
                    if ($(popups.get(i)).attr("id") == this.myContentDiv.attr("id")) {
                        break;
                    }

                }
                i = i - 1;


            }
            else {
                this.myContenter = window;
                this.myBgDiv = this.$bgDiv;
                this.myContentDiv = this.$contentDiv;
                this.myDragBarDiv = this.$dragBarDiv;
            }

            var myCloseBtn = this.myDragBarDiv.find('button[onclick]');
            var myContentDiv = this.myContentDiv;
            var myBgDiv = this.myBgDiv;
            var iframeId;
            if (isAncestor) {
                var oldmyContentDiv = popups.eq(i) //找出上一个以$contentDiv.attr("id")结尾的ID,并且是当前弹出窗口的前一个
                //alert(oldmyContentDiv.find("iframe[name!=BgIframe]").get(0).html());
                iframeId = oldmyContentDiv.find("iframe[name!=BgIframe]").attr("id", "iframe" + oldmyContentDiv.attr("id")).attr("name", "iframe" + oldmyContentDiv.attr("id")).attr("id");
            }

            var _onClose = this.onClose == null ? ClosePopup : this.onClose;
            var ISIE6 = this.ISIE6;

            myCloseBtn.attr('onclick', '').click(function () {

                $(myContentDiv).hide();
                $(myBgDiv).css('display', 'none');

                if (isAncestor) {
                    if (_onClose && iframeId) {
                        _onClose(iframeId);
                    }
                }
                else {
                    _onClose();
                }

                //我这里没有IE6的环境没有办法测试了
                if (ISIE6) { $("select").each(function () { if (!IsIn(this, myContentDiv.attr("id"))) { $(this).attr("disabled", "") }; }) };
                myContentDiv.find("iframe[name!='BgIframe']").attr("src", this.LoadingPath);
            });

            this.myBgDiv.css({ "height": $(this.myContenter.document).height() - 4, "display": "block" });
            //this.myContentDiv.css("display", "block");
            this.SetPosition(this.X, this.Y);
            this.SetDragable();
            this.myContentDiv.slideDown(500);
        }
    },
    onClose: null,
    Hide: function () {
        if (this.ready) {
            if (this.dragable && this.$dragBarDiv != null) { this.$dragBarDiv.unbind("mousedown"); }
            this.$contentDiv.css({ "display": "none", "top": "0px", "left": "0px" });
            // $($contentDiv).remove();
            this.$bgDiv.css({ "display": "none" });
            //if (this.timer != null) { clearInterval(this.timer); }
            if (this.ISIE6) { var $contentDiv = this.$contentDiv; $("select").each(function () { if (!IsIn(this, $contentDiv.attr("id"))) { $(this).attr("disabled", "") }; }) };
            this.$contentDiv.find("iframe[name!='BgIframe']").attr("src", this.LoadingPath);
        }
    },
    Create: function (ContentID, DragBarID) {
        var html = ' <div id="' + ContentID + '" class="modal-dialog" style="display: none;">';
        html += ' <div class="modal-content">';
        html += ' <div id="' + DragBarID + '"  class="modal-header">';
        html += ' <button type="button" class="close" onclick="">&times;</button>';
        html += ' <h4 class="modal-title"><span id="PopupTitle_0"></span></h4>';
        html += ' </div>';
        html += ' <div class="modal-body">';
        html += ' <iframe allowtransparency="true" src="../loading.htm" frameborder="0" scrolling="auto"></iframe>';
        html += ' </div>';
        html += ' </div>';
        html += ' </div>';
        $(document.body).append(html);
    }
}

function SonClosePopup() {
    $($(parent.document.body).find(".modal-header").get($(parent.document.body).find(".modal-header").length - 1)).find('button').click();
}